package com.jcfk.eam.controller.bd;

import cn.hutool.core.util.StrUtil;
import com.jcfk.common.business.log.annotation.RequestLogHeader;
import com.jcfk.common.web.domain.DataPage;
import com.jcfk.common.web.domain.ReqPageParam;
import com.jcfk.common.web.domain.RetPage;
import com.jcfk.common.web.domain.RetResult;

import com.jcfk.eam.domain.dto.base.EamCommonDocDTO;
import com.jcfk.eam.domain.excel.base.EamCommonDocExcel;
import com.jcfk.eam.domain.query.base.EamCommonDocQuery;
import com.jcfk.eam.service.base.EamCommonDocService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;

import com.jcfk.common.core.utils.ExcelUtils;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 资产通用文档
 *
 * @author libinghai
 * @email 35738025@gqq.com
 * @date 2024-08-16
 */
@Api(tags = {"资产通用文档管理"})
@RestController
@RequestMapping("eamCommonDoc")
public class EamCommonDocController {
    @Autowired
    private EamCommonDocService eamCommonDocService;

    /**
     * 分页列表
     */
    @ApiOperation("分页查询资产通用文档")
    @PostMapping("/queryByPage")
    public RetPage<EamCommonDocDTO> queryByPage(@RequestBody ReqPageParam<EamCommonDocQuery> requestParam) {
        DataPage<EamCommonDocDTO> page = new DataPage<EamCommonDocDTO>((long) requestParam.getCurrent(),
                (long) requestParam.getSize(),
                requestParam.getSort().getOrderItem(),
            EamCommonDocDTO.class);
        page = eamCommonDocService.queryByPage(page, requestParam.getData());

        return new RetPage<>(page);
    }


    /**
     *  资产通用文档
     * 根据id 查询详情
     */
    @ApiOperation("查询资产通用文档")
    @GetMapping("/get/{id}")
    public RetResult<EamCommonDocDTO> get(@PathVariable("id") String id) {
            EamCommonDocDTO item = eamCommonDocService.getView(id);
        return RetResult.ok(item);
    }

    /**
     * 添加/更新管理资产通用文档
     */
    @ApiOperation("保存资产通用文档")
    @PostMapping("/save")
    @RequestLogHeader(modular = "资产通用文档管理", businessType = "保存")
    public RetResult<?> save(@RequestBody @Valid EamCommonDocDTO eamCommonDoc) {
        // 数据校验
        RetResult ret = eamCommonDocService.isValid(eamCommonDoc);
        if (!ret.getCode().equals(RetResult.SUCCESS)) {
            return ret;
        }
        if (StrUtil.isEmpty(eamCommonDoc.getId())){
            // 新增
            eamCommonDocService.insert(eamCommonDoc);
        } else{
            // 更新
            eamCommonDocService.update(eamCommonDoc);
        }
        return RetResult.ok();

    }

    /**
     *  批量删除资产通用文档
     */
    @ApiOperation("批量删除资产通用文档")
    @PostMapping("/delete")
    @RequestLogHeader(modular = "资产通用文档管理", businessType = "批量删除")
    public RetResult<?> delete(@RequestBody EamCommonDocDTO eamCommonDoc) {
        return RetResult.ok(eamCommonDocService.removeBigBatch(eamCommonDoc.getIds()));
    }


    @PostMapping("export")
    @ApiOperation("导出")
    public void export(@RequestBody EamCommonDocQuery requestParam, HttpServletResponse response) throws Exception {
        List<EamCommonDocDTO> list = eamCommonDocService.getList(requestParam);
        ExcelUtils.exportExcelToTarget(response, null, list, EamCommonDocExcel.class);
    }

    /**
     *  删除资产通用文档
     */
    @ApiOperation("删除资产通用文档")
    @PostMapping("/deleteItem/{id}")
    @RequestLogHeader(modular = "资产通用文档管理", businessType = "删除")
    public RetResult<?> deleteItem(@PathVariable("id") String id) {
        eamCommonDocService.deleteItem(id);
        return RetResult.ok();
    }
}
